package com.zjut.an.util.acquisition;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zjut.an.acquisition.dao.AnAcquisition;
import com.zjut.an.acquisitionhistory.dao.AnAcquisitionHistory;
import com.zjut.an.util.tools.SpringContextUtil;
import com.zjut.an.util.system.Config;

public class AcquQuartzTask implements Job{
	
	static{
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler;
		try {
			scheduler = schedulerFactory.getScheduler();
			scheduler.start();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static final String ACQUISITION_JOB_GROUP_STRING = "ACQUISITION_JOB_GROUP";
	public static final String ACQUISITION_TRIGGER_GROUP_STRING = "ACQUISITION_TRIGGER_GROUP";
	public static final String ACQUISITION_INFO_STRING = "ACQUISITION_INFO";
	public static final String ACQUISITION_PLANS_STRING = "ACQUISITION_PLANS";
	
	public static ConcurrentHashMap<Integer, Boolean> lockMap = new ConcurrentHashMap<Integer, Boolean>();
	
	private static final Logger log = LoggerFactory.getLogger(AcquQuartzTask.class);
	
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		//
	}
	
	/**
	 * 增加调度任务
	 * @param model 基础信息
	 * @return 添加成功返回true
	 */
	public static boolean addAcquisitionJob(AnAcquisition model, String[] plans) {
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler;
		try {
			scheduler = schedulerFactory.getScheduler();
			JobDetail jobDetail = newJob(AcquQuartzTask.class).withIdentity(model.getAcId().toString(),ACQUISITION_JOB_GROUP_STRING).build();
			jobDetail.getJobDataMap().put(ACQUISITION_INFO_STRING, model.getAcId());
			jobDetail.getJobDataMap().put(ACQUISITION_PLANS_STRING, plans);
			simpleSchedule();
			SimpleTrigger simpleTrigger = newTrigger()
					.withIdentity(model.getAcId().toString(),ACQUISITION_TRIGGER_GROUP_STRING)
					.withSchedule(SimpleScheduleBuilder
							.repeatSecondlyForever(10))
							//.withIntervalInMilliseconds(model.getPauseTime())
					.startNow()
					.build();
			scheduler.scheduleJob(jobDetail, simpleTrigger);		
			log.debug("add acquisition task :"+model.getAcqName());
			return true;
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage());
			return false;
		}
	}
	
	/**
	 * 暂停调度任务
	 * @param model
	 * @return
	 */
	public static boolean pauseAcquest(AnAcquisition model){
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler;
		try {
			scheduler = schedulerFactory.getScheduler();
			JobKey sendMailJobKey = new JobKey(model.getAcId().toString(),ACQUISITION_JOB_GROUP_STRING);
			scheduler.pauseJob(sendMailJobKey);
			log.debug("pause acquisition task :"+model.getAcqName());
			return true;
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.debug(e.getMessage());
			return false;
		}
	}
	
	/**
	 * 停止调度任务
	 * @param model
	 * @return
	 */
	public static boolean stopAcquest(AnAcquisition model){
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler;
		try {
			scheduler = schedulerFactory.getScheduler();
			JobKey sendMailJobKey = new JobKey(model.getAcId().toString(),ACQUISITION_JOB_GROUP_STRING);
			scheduler.deleteJob(sendMailJobKey);
			log.debug("delete acquisition task :"+model.getAcqName());
			return true;
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.debug(e.getMessage());
			return false;
		}
	}
	
	/**
	 * 继续调度任务
	 * @param model
	 * @return
	 */
	public static boolean resumeAcquest(AnAcquisition model){
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler;
		try {
			scheduler = schedulerFactory.getScheduler();
			JobKey sendMailJobKey = new JobKey(model.getAcId().toString(),ACQUISITION_JOB_GROUP_STRING);
			scheduler.resumeJob(sendMailJobKey);
			log.debug("resume acquisition task :"+model.getAcqName());
			return true;
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.debug(e.getMessage());
			return false;
		}
	}
	
	public static boolean hasJob(AnAcquisition model) throws SchedulerException{
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		return scheduler.checkExists(new JobKey(model.getAcId().toString(),ACQUISITION_JOB_GROUP_STRING));
	}

}
